﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Net.Mail;
using SMBL.Interface.LifeCycle;

namespace SMBL
{
    namespace Global
    {
        /// <summary>
        /// Basic Error System
        /// @Author: Push
        /// The basic error system is used to 
        /// catch the exception occurred when try to 
        /// start up the website
        /// This system will send an error report to a specified email address
        /// </summary>
        public class BasicErrorSystem
        {
            //////////////////////////////////////////////////////////////////////////
            // The mail Information
            //////////////////////////////////////////////////////////////////////////

            private static readonly String mailAccount = "smbl.error@gmail.com";
            private static readonly String mailPassword = "sundaymakersystem";
            private static readonly String mailServer = "smtp.gmail.com";
            private static readonly Int32 mailPort = 587;

            // The Report Mail
            private static MailMessage reportMail;

            //////////////////////////////////////////////////////////////////////////
            // Failed to Send the report mail
            //////////////////////////////////////////////////////////////////////////
            private static List<String> unsentReport;
            public static List<String> UnsentReport
            {
                get { return unsentReport; }
            }

            /// <summary>
            /// Initialize the System
            /// </summary>
            public static void Initialize()
            {
                //throw new NotImplementedException();
                BasicErrorSystem.reportMail = new MailMessage(
                    BasicErrorSystem.mailAccount, BasicErrorSystem.mailAccount);
                BasicErrorSystem.reportMail.Subject = "Error Report";
                BasicErrorSystem.reportMail.IsBodyHtml = true;

                // Unsent report
                BasicErrorSystem.unsentReport = new List<string>();
            }

            /// <summary>
            /// Generate an error report
            /// bind to the report mail
            /// invoke sendMail();
            /// @Author: Push
            /// </summary>
            /// <param name="theException"></param>
            public static void UnhandledError(Exception theException)
            {
                BasicErrorSystem.reportMail.Body = (new ErrorReport(theException)).ToString();
                BasicErrorSystem.sendMail();
            }

            /// <summary>
            /// Generate an error report
            /// bind to the report mail
            /// invoke sendMail();
            /// @Author: Push
            /// </summary>
            /// <param name="theErrorList"></param>
            public static void UnhandledError(ArrayList theErrorList)
            {
                BasicErrorSystem.reportMail.Body = (new ErrorReport(theErrorList)).ToString();
                BasicErrorSystem.sendMail();
            }

            private static void sendMail()
            {
                try
                {
                    SmtpClient smtp = new SmtpClient(
                        BasicErrorSystem.mailServer,    // smtp.gmail.com
                        BasicErrorSystem.mailPort);     // 587
                    smtp.UseDefaultCredentials = false;
                    smtp.Credentials = new System.Net.NetworkCredential(
                        BasicErrorSystem.mailAccount,   // Account Information
                        BasicErrorSystem.mailPassword);
                    smtp.EnableSsl = true;              // Google Need SSL Authorization
                    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                    // Send the report mail
                    smtp.Send(BasicErrorSystem.reportMail);
                }
                catch (System.Exception e)
                {
                    BasicErrorSystem.unsentReport.Add(BasicErrorSystem.reportMail.Body);
                    BasicErrorSystem.unsentReport.Add((new ErrorReport(e)).ToString());
                }
            }
        }
    }
}